/**
 * lilang
 * MailAction.java
 * 2013-9-26下午2:13:10
 */
package com.cq.oa.web.pof.action;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.session.SessionManger;
import oa.base.util.Constants;
import oa.base.web.Action.BaseAction;

import org.apache.log4j.Logger;

import com.cq.oa.domain.pof.CalendarEntry;
import com.cq.oa.service.pof.CalendarService;

/**
 * @author lilang
 *
 */
public class PofCalendarAction extends BaseAction<CalendarEntry>{

	/**
	 * lilang
	 * 2013-9-26下午2:25:17
	 */
	private static final long serialVersionUID = -7440785461596750089L;
	
	protected static final  Logger logger = Logger.getLogger(PofCalendarAction.class);
	
	
	private CalendarService calendarService;
	
	
	private Long beginTime;
	
	private Long endTime;
	

	public Long getBeginTime() {
		return beginTime;
	}



	public void setBeginTime(Long beginTime) {
		this.beginTime = beginTime;
	}


	public Long getEndTime() {
		return endTime;
	}



	public void setEndTime(Long endTime) {
		this.endTime = endTime;
	}





	public void setCalendarService(CalendarService calendarService) {
		this.calendarService = calendarService;
	}





	/* (non-Javadoc)
	 * @see oa.base.web.Action.BaseAction#getEntityClass()
	 */
	@Override
	protected Class<CalendarEntry> getEntityClass() {
		return CalendarEntry.class;
	}
	
	
	public String findCalendar() throws Exception {
		filterMap.put("orgSid", SessionManger.getOrgId());
		filterMap.put("initiatorSid", SessionManger.getUserId());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(endTime * 1000));
		calendar.add(Calendar.DATE, 1);
		Date[] between = new Date[2];
		between[0] = new Date(beginTime * 1000);
		between[1] = calendar.getTime();
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(Constants.WHERE_BETWEEN+"beginTime", between);
		params.put(Constants.WHERE_BETWEEN+"endTime", between);
		filterMap.put(Constants.WHERE_OR, params);
		List<CalendarEntry> users=calendarService.findParByList(filterMap, linkMap,shortMap);
		this.renderJsonSuccess(jsonUtils.getExtArrayJson(users, new String[] {}));
		return JSON;
	}
	
	public String operate(){
		if(null!=entity.getSid()){
			entity = calendarService.findByParmkeys(entity.getSid());
		}
		return "operate";
	}
	
	public String save(){
		if(null!=entity.getSid()){
			this.calendarService.update(entity);
		}else{
			entity.setInitiatorSid(SessionManger.getUserId());
			entity.setInitiatorCname(SessionManger.getCname());
			entity.setInitiatorEmail(SessionManger.getEmail());
			this.calendarService.create(entity);
		}
		this.renderJsonSuccess();
		return JSON;
	}
	
	/**
	 * 详情
	 * @return
	 * lilang 
	 * 2013-10-10下午3:09:42
	 */
	public String detail(){
		try {
			entity = this.calendarService.findByParmkeys(entity.getSid());
		} catch (Exception e) {
			logger.error(e);
		}
		return "detail";
	}
	
}
